﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using BusinessObjects;

#endregion

namespace FastTrackCabService.AdminPanel
{
    public partial class Drivers : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlSubsidiary.Items.Add(new ListItem("All", "0"));

                ibAdd.Visible = Roles.GetRolesForUser(User.Identity.Name).Contains("Manager");

                foreach (var subsidiary in SubsidiaryBLL.GetAllSubsidiaries().OrderBy(sub => sub.CityId))
                {
                    var city = CityBLL.GetCityById(subsidiary.CityId);
                    var item = new ListItem(city.Name, subsidiary.SubsidiaryId.ToString());
                    ddlSubsidiary.Items.Add(item);
                }

                LoadGridview();
            }
        }

        private void LoadGridview()
        {
            var displayList = DriverBLL.GetAllDrivers()
                .Join(StaffBLL.GetAllStaffs(), drive => drive.StaffId, staff => staff.StaffId, (drive, staff) => new
                {
                    staff.StaffId,
                    staff.FirstName,
                    staff.TelephoneNo,
                    drive.LicenseNo,
                    drive.Availability,
                    staff.SubsidiaryId
                });

            if (ddlSubsidiary.SelectedIndex != 0)
            {
                displayList = displayList.Where(v => v.SubsidiaryId == Convert.ToByte(ddlSubsidiary.SelectedValue));
            }

            if (ddlAvailability.SelectedIndex != 0)
            {
                displayList =
                    displayList.Where(v => v.Availability == Convert.ToBoolean(ddlAvailability.SelectedValue));
            }

            gvDrivers.DataSource = displayList.ToList().OrderByDescending(s => s.StaffId).ToList();
            gvDrivers.DataBind();
        }

        protected void gvDrivers_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDrivers.PageIndex = e.NewPageIndex;
            LoadGridview();
        }

        protected void ibtnSearch_OnClick(object sender, ImageClickEventArgs e)
        {
            int staffId;
            ddlAvailability.SelectedIndex = 0;
            ddlSubsidiary.SelectedIndex = 0;
            if (!int.TryParse(tbSearch.Text, out staffId))
            {
                LoadGridview();
            }
            else
            {
                var staff = StaffBLL.GetStaffById(staffId);

                if (staff == null)
                {
                    gvDrivers.DataSource = null;
                }
                else
                {
                    var driver = new Driver(staff)
                    {
                        LicenseNo = DriverBLL.GetLicenceNo(staffId)
                    };

                    gvDrivers.DataSource = driver.LicenseNo == null ? null : new List<Driver> {driver};
                }

                gvDrivers.DataBind();
            }
        }

        protected void ibDelete_OnClick(object sender, ImageClickEventArgs e)
        {
            var staffId = Convert.ToInt32(Request.QueryString["StaffId"]);
            StaffBLL.RemoveStaff(staffId);
            LoadGridview();
        }

        protected void ddlSubsidiary_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            tbSearch.Text = string.Empty;
            LoadGridview();
        }

        protected void ddlAvailability_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            tbSearch.Text = string.Empty;
            LoadGridview();
        }
    }
}